<template>
  <div>
    <h1>任务管理</h1>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        fixed
        prop="taskId"
        label="任务ID"
        width="150">
      </el-table-column>
      <el-table-column
        prop="taskName"
        label="任务名称"
        width="120">
      </el-table-column>
      <el-table-column
        prop="taskNum"
        label="任务周期"
        width="120">
      </el-table-column>
      <el-table-column
        prop="taskype"
        label="任务类型"
        width="120">
        <template slot-scope="obj">
          {{aa[obj.row.taskype]}}
        </template>
      </el-table-column>
      <el-table-column
        prop="taskStarttime"
        label="开始时间"
        width="300">
      </el-table-column>
      <el-table-column
        prop="taskEndtime"
        label="结束时间"
        width="120">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
          <el-button @click="tjtask(scope.row)" type="text" size="small">提交任务</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[1, 2, 3, 5]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totel">
      </el-pagination>
    </div>
    <div>
      <el-dialog title="提交任务" :visible.sync="dialogFormVisible">
        <el-form :model="tbTaskSituation">
          <el-form-item label="我的头像" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:7000/task/task/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="tbTaskSituation.situationPic" :src="tbTaskSituation.situationPic" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="toadd">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { taskDelete, taskpage,taskSituation } from "@/api/task";

  export default {
    name: 'Index',
    data() {
      return {
        dialogFormVisible: false,
        formLabelWidth: '120px',
        aa:{
          1:"运动",
          2:"食谱",
          3:"设备",
          4:"学习",
        },
        tableData: [],
        tbTaskPage: {
          current: 1,
          pagesize: 5,
        },
        totel: 0,
        tbTaskd:{

        },
        tbTaskSituation:{

        },
      }
    },mounted() {
      this.taskpage()
    },methods: {
      taskpage(){
        taskpage(this.tbTaskPage).then((res)=>{
          console.log(res)
          this.tableData=res.data.records
          this.totel=res.data.total
        })
      },
      handleSizeChange(val) {
        this.tbTaskPage.pagesize=val
        console.log(`每页 ${val} 条`);
        this.taskpage()
      },
      handleCurrentChange(val) {
        this.tbTaskPage.current=val
        console.log(`当前页: ${val}`);
        this.taskpage()
      },
      handleClick(row){
        this.tbTaskd.taskId=row.taskId;
        alert(row.taskId)
        alert(this.tbTaskd.taskId)
        taskDelete(this.tbTaskd).then((res)=>{
          this.taskpage()
          console.log(res)
        })
      },
      tjtask(row){
        this.tbTaskSituation.situationTid=row.taskId;
        this.dialogFormVisible=true;
      },
      toadd(){
        taskSituation(this.tbTaskSituation).then((res)=>{
          console.log(res)
          this.dialogFormVisible=false;
        })
      },
      handleAvatarSuccess(res, file) {
        this.tbTaskSituation.situationPic = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

